package com.lysj.bhp.app.service;

import com.lysj.bhp.app.base.AppContent;
import com.lysj.bhp.user.domain.User;
import com.lysj.bhp.util.web.ParamUtil;
import com.lysj.bhp.util.web.Resp;
import com.lysj.bhp.wallet.domain.BhpcWallet;
import com.lysj.bhp.wallet.domain.BitCoinWallet;
import com.lysj.bhp.wallet.domain.EthereumWallet;
import com.lysj.bhp.withdraw.domain.Poundage;
import com.lysj.bhp.withdraw.domain.WithDraw;
import com.lysj.bhp.withdraw.domain.WithDrawAddress;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Date;

@Service
@Transactional
public class AppWithDrawApplyService extends AppContent {

    public Resp withdrawApplyAdd(String email, String withDrawAddressId, BigDecimal amount, int walletType, String tradePassword) {

        if (ParamUtil.isBlack(withDrawAddressId)) {
            return new Resp(Resp.PARAM_ERROR, "请输入提币地址");
        }
        WithDrawAddress withDrawAddress = withDrawAddressRepository.findOne(Long.valueOf(withDrawAddressId));
        if (null == withDrawAddress) {
            return new Resp(Resp.PARAM_ERROR, "地址错误");
        }
        if (BigDecimal.ZERO.compareTo(amount) == 1) {  //提币数量小于0
            return new Resp(Resp.PARAM_ERROR, "提币数量错误");
        }
        if (ParamUtil.isBlack(tradePassword)) {
            return new Resp(Resp.PARAM_ERROR, "交易密码不能为空");
        }

        WithDraw withDraw = new WithDraw();
        Poundage poundage = null;
        if (100 == walletType) {
            poundage = poundageRepository.findByTypeAndInfoStatus(walletType, 0);
        } else {
            //获取提币手续费
            poundage = poundageRepository.findByType(walletType);
        }
        BigDecimal transferFee = new BigDecimal(BigInteger.ZERO);// 提币手续费默认为0
        // 平台设置手续费
        if (!ParamUtil.isBlack(poundage)) {
            transferFee = poundage.getTransferFee();
            if ((amount.compareTo(poundage.getMiniNumber()) == -1)) { //手续费
                return new Resp(Resp.PARAM_ERROR, "提币数量最少为" + poundage.getMiniNumber().setScale(6, BigDecimal.ROUND_DOWN));
            }
        }
        User user = userRepository.findByEmailAndStatus(email, 1);
        if (null == user) {
            return new Resp("提币错误");
        }

        if (!ParamUtil.md5(tradePassword).equals(user.getTradePassword())) {
            return new Resp(Resp.PARAM_ERROR, "交易密码有误");
        }
        switch (walletType) {  //10:比特币;20:以太坊;100:BHPC;110:BHPC_Bouns
            case 100:
                BhpcWallet bhpcWallet = bhpcWalletRepository.findByUserId(user.getUserId());

                if (null == bhpcWallet) {
                    return new Resp(Resp.PARAM_ERROR, "您的BHPC数量不足");
                }

                if ((bhpcWallet.getAvailBalance().subtract(amount)).compareTo(new BigDecimal(BigInteger.ZERO)) == -1) { //钱包的可用数额跟提币数量进行比较
                    return new Resp(Resp.PARAM_ERROR, "BHPC数量不足");
                }
                withDraw.setUserId(user.getUserId());
                withDraw.setEmail(user.getEmail());
                withDraw.setAddress(withDrawAddress.getAddress());
                withDraw.setType(100);
                withDraw.setAmount(amount);
                withDraw.setFeeAmount(transferFee);
                withDraw.setCreateDate(new Date());
                withDraw.setStatus(0);
                withDraw.setCheckStatus(3);
                withDraw.setName("BHPC");
                withDraw.setChineseName("算力现金");
                withDraw.setAuthentication("成功");
                withDraw.setOperationType(1); //app端发起
                //获取该用户的BHPC钱包,生成冻结余额,修改可用余额
                bhpcWallet.setAvailBalance((bhpcWallet.getAvailBalance().subtract(amount)));
                bhpcWallet.setFreezeBalance(bhpcWallet.getFreezeBalance().add(amount));
                bhpcWalletRepository.save(bhpcWallet);
                withdrawRepository.save(withDraw);
                break;
            case 10:
                BitCoinWallet bitCoinWallet = bitcoinWalletRepository.findByUserId(user.getUserId());

                if (null == bitCoinWallet) {
                    return new Resp(Resp.PARAM_ERROR, "您的BTC数量不足");
                }

                if ((bitCoinWallet.getAvailBalance().subtract(amount)).compareTo(new BigDecimal(BigInteger.ZERO)) == -1) { //手续费
                    return new Resp(Resp.PARAM_ERROR, "BTC提币数量不足");
                }

                withDraw.setUserId(user.getUserId());
                withDraw.setEmail(user.getEmail());
                withDraw.setAddress(withDrawAddress.getAddress());
                withDraw.setType(10);
                withDraw.setAmount(amount);
                withDraw.setFeeAmount(transferFee); //需要修改
                withDraw.setCreateDate(new Date());
                withDraw.setStatus(0);
                withDraw.setCheckStatus(3);
                withDraw.setName("BTC");
                withDraw.setChineseName("比特币");
                withDraw.setAuthentication("成功");
                withDraw.setOperationType(1); //app端发起
                //获取该用户的BHPC钱包,生成冻结余额,修改可用余额
                bitCoinWallet.setAvailBalance(bitCoinWallet.getAvailBalance().subtract(amount));
                bitCoinWallet.setFreezeBalance(bitCoinWallet.getFreezeBalance().add(amount));
                bitcoinWalletRepository.save(bitCoinWallet);
                withdrawRepository.save(withDraw);
                break;
            case 20:
                EthereumWallet ethereumWallet = ethereumWalletRepository.findByUserId(user.getUserId());

                if (null == ethereumWallet) {
                    return new Resp(Resp.PARAM_ERROR, "您的ETH数量不足");
                }

                if ((ethereumWallet.getAvailBalance().subtract(amount)).compareTo(new BigDecimal(BigInteger.ZERO)) == -1) { //手续费
                    return new Resp(Resp.PARAM_ERROR, "ETH提币数量不足");
                }
                withDraw.setUserId(user.getUserId());
                withDraw.setEmail(user.getEmail());
                withDraw.setAddress(withDrawAddress.getAddress());
                withDraw.setType(20);
                withDraw.setAmount(amount);
                withDraw.setFeeAmount(transferFee);
                withDraw.setCreateDate(new Date());
                withDraw.setStatus(0);
                withDraw.setCheckStatus(3);
                withDraw.setName("ETH");
                withDraw.setChineseName("以太坊");
                withDraw.setAuthentication("成功");
                withDraw.setOperationType(1); //app端发起
                //获取该用户的BHPC钱包,生成冻结余额,修改可用余额
                ethereumWallet.setAvailBalance((ethereumWallet.getAvailBalance().subtract(amount)));
                ethereumWallet.setFreezeBalance(ethereumWallet.getFreezeBalance().add(amount));
                ethereumWalletRepository.save(ethereumWallet);
                withdrawRepository.save(withDraw);
                break;
            default:
                return new Resp(Resp.PARAM_ERROR, "提币类型错误");
        }
        return new Resp("提币申请成功");
    }
}
